package com.zslc.securitymonitoring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class ThreadPoolConfig {
    @Bean("asyncDataExecutor")
    public TaskExecutor AsyncDataExecutor() {
        return getThreadPoolTaskExecutor("不同告警信息独立查库线程--", 5, 20);
    }

    private TaskExecutor getThreadPoolTaskExecutor(String namePrefix, int coreSize, int maxSize) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        //配置核心线程数
        executor.setCorePoolSize(coreSize);

        //配置最大线程数
        executor.setMaxPoolSize(maxSize);

        executor.setKeepAliveSeconds(60);

        //配置队列大小
        executor.setQueueCapacity(100);

        executor.setThreadNamePrefix(namePrefix);

        //拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        executor.setWaitForTasksToCompleteOnShutdown(true);

        //执行初始化
        executor.initialize();
        return executor;
    }
}
